<!--
 * @Author: jiangyg email@jiangyg.com
 * @Date: 2022-08-14 15:31:12
 * @LastEditors: jiangyg email@jiangyg.com
 * @LastEditTime: 2022-11-21 06:24:49
 * @FilePath: /frontend/mall-frontend/src/views/modules/admin/system/depart-user-relation.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog :title="`新增部门[${depart.name}]用户列表`" width="50%" draggable 
              :close-on-click-modal="false" v-model="visible" @open="submitted = false">
    <el-form :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit(dataFormRef)" label-width="80px">
      <div style="text-align: center">
        <el-transfer
          style="text-align: left; display: inline-block"
          v-model="selectData"
          :titles="['未分配用户', '已分配用户']"
          filterable
          :data="dataList"
          empty-text="木有数据~"
          :format="{ noChecked: '${total}', hasChecked: '${checked}/${total}' }"
        >
        </el-transfer>
      </div>
    </el-form>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted || selectData.length <= 0">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import { ElMessage, FormInstance } from "element-plus";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";

// 弹窗是否显示
const visible = ref(false);

// 部门信息
const depart = reactive({ 
  id: "", 
  name: "", 
});

// 提交按钮是否禁用
const submitted = ref(false);

// 选中集合
const selectData = ref([]);

// 未分配部门的用户列表
const dataList = ref([]);

// 数据校验
const dataRule = reactive({});

// 表单引用
const dataFormRef = ref<FormInstance>();

// 父级组件的自定义事件
const emit = defineEmits(['initUserTableData']);

// 表单初始化
const initForm = (d) => {
  depart.id = d.id;
  depart.name = d.name;
  selectData.value = [];
  http
    .post({
      url: "/depart/unallocated-user-list",
    })
    .then(async ({ data }) => {
      if (http.success(data)) {
        visible.value = true;
        await nextTick();
        for (let i = 0; i < data.body.length; i++) {
          dataList.value.push({
            key: data.body[i].id,
            label: `${data.body[i].username}`,
          });
        }
      }
    });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  if (selectData.value.length <= 0) {
    ElMessage.error("请选择要分配部门的用户！");
    return;
  }
  submitted.value = true;
  http
    .post({
      url: "/depart/save-relation",
      data: {
        departId: depart.id,
        userIds: selectData,
      },
    })
    .then(async ({ data }) => {
      if (http.success(data, true, true)) {
        visible.value = false;
        emit("initUserTableData");
        return;
      }
      submitted.value = false;
    });
};

// 暴露方法
defineExpose({ initForm });
</script>
